﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
using System.Collections.Generic;
namespace WPFExcelReport
{

    public partial class TQORDER : ViewBase
    {  

        #region ==QCORDER===========
        public static readonly DependencyProperty QCORDERProperty = DependencyProperty.Register("QCORDER",
            typeof(string), typeof(TQORDER),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQCORDERChanged));
        public string QCORDER
        {
            get
            {
                return (string)GetValue(QCORDERProperty);
            }
            set
            {
                SetValue(QCORDERProperty, value);
                ((VQORDER)MV).QCORDER = value;
                LOADQC();
            }
        }
        private static void OnQCORDERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQORDER;
            if (src != null)
            {
                src.QCORDER = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwQC===========
        public static readonly DependencyProperty dwQCProperty = DependencyProperty.Register("dwQC",
            typeof(DataRow), typeof(TQORDER),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQCChanged));
        public DataRow dwQC
        {
            get
            {
                return (DataRow)GetValue(dwQCProperty);
            }
            set
            {
                SetValue(dwQCProperty, value);
            }
        }
        private static void OdwQCChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQORDER;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQC = row;
            }
        }
        #endregion

        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TQORDER),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VQORDER)MV).QPLAN = value; 
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQORDER;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

       /* #region ==YCOUNT===========
        public static readonly DependencyProperty YCOUNTProperty = DependencyProperty.Register("YCOUNT",
            typeof(string), typeof(TQORDER),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnYCOUNTChanged));
        public string YCOUNT
        {
            get
            {
                return (string)GetValue(YCOUNTProperty);
            }
            set
            {
                SetValue(YCOUNTProperty, value);
                ((VQORDER)MV).YCOUNT = value;
            }
        }
        private static void OnYCOUNTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQORDER;
            if (src != null)
            {
                src.YCOUNT = (string)e.NewValue;
            }
        }
        #endregion*/

        #region ==dwReq===========
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
            typeof(DataRow), typeof(TQORDER),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));
        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQORDER;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion
         
        public TQORDER()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VQORDER();
            MESSAGES += "QCNEW;";
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQC = dwMaster;
            if (dwQC == null)
            {
                KEY = "";
            }
            else
            {
                KEY = dwQC["QOKORDER"].ToString();
            }
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            { 
                
            }
        }
       
        public void LOADQC()
        {
            TBMain.Clear();
            if (QCORDER == "000000000")
            {
                return;
            } 
            DataRow dwData = TBMain.NewRow();
            dwData["QOKMANDANT"] = app.Mandant;
            dwData["QOKORDER"] = QCORDER;
            DataSet SResult = ((VQORDER)MV).QueryQORDER(dwData, 3);
            MergeData(SResult, true); 

        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwQC == null)
                return;
            dwQC["QOKAENB"] = app.User;
            dwQC["QOKAEND"] = vm.date;
            ((VQORDER)MV).UpdateQORDER(dwQC, app.company, 0);
            VQOSTEP.Instance.UPDTEBYGB33(RUNID, dwReq, true);
            base.SaveCommand(sender, e);
        }
        
        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            if (dwQC == null)
            {
                MessageBox.Show("不支持无条件创建检测单");
                return;
            } 
            ((VQORDER)MV).COPYQC(dwQC,dwReq);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwQC == null)
                return;
            ((VQORDER)MV).DeleteQORDER(dwQC,1);
            dwData = VQOSTEP.Instance.TBMain.NewRow();
            dwData["QOEMANDANT"]=app.Mandant;
            dwData["QOEORDER"]=dwQC["QOKORDER"];
            VQOSTEP.Instance.DeleteQOSTEP(dwData, 2);
            base.DeleteCommand(sender,e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }
    }
}
